"use strict";

import $R from '../../router'
import $axios from 'axios'
import $L from './storage'
import $P from './public'
import $global from '../../../globalConfig'
import $E from './encryption'

//配置跨域
if ($global.ISLOCALORAPI) {
    $axios.defaults.baseURL = $global.BASE_URL;
    $axios.defaults.headers.post['Content-Type'] = $global.CONTENT_TYPE;
}

/**
 * 发送ajax请求
 * @param obj
 */
let ajax = {
    http: function (obj) {
        let token = $L.Get('token') || '';
        // if (!token) {
        //     $R.push({ path: '/login' });
        // }
        obj.params = Object.assign({ token }, obj.params);

        // if(!$global.HTTP_CRYPTO) {
        //     console.group("请求数据");
        //     console.log("请求地址", obj.url);
        //     console.log("请求参数", obj);
        // }

        if (obj.params) {
            obj.header = Object.assign(obj.header || {}, { 'Content-Type': 'application/json;charset=UTF-8' });
            //是否加密axios参数
            if ($global.HTTP_CRYPTO) obj.params = $E.encrypt(obj.params)
        }

        $axios({
            url: obj.url,
            method: obj.method || 'post',
            data: obj.form || obj.params || undefined,
            headers: obj.header || undefined,
        }).then((res) => {
            if (typeof (obj.newThen) === 'function') {
                obj.newThen();
            }
            if (res.status === 200) {
                let resData;
                resData = res.data && res.data.body ? $E.decrypt(res.data.body) : res.data;

                // if(!$global.HTTP_CRYPTO) console.info("接收数据", resData);

                if (resData.code === 200) {
                    if (typeof (obj.success) === 'function') {
                        obj.success(resData);
                    }
                } else if (resData.code === 201) {
                    $L.Clear(3);
                    $P.Msg('token失效或账号在其他设备登录,请重新登录！', 3);
                    $R.push({ path: '/login' });
                } else {
                    $P.Msg(resData.msg, 4);
                    if (typeof (obj.error) === "function") {
                        obj.error(res);
                    }
                }
                return false;
            } else {
                $P.Msg("异常HTTP错误：" + res.status, 4);
                if (typeof (obj.error) === "function") {
                    obj.error(res);
                }
                return false;
            }
        }).catch((error) => {
            $P.Msg("访问中断，程序出错！", 4);
            if (!$global.HTTP_CRYPTO) console.error("网络中断，程序出错！", error);

            if (typeof (obj.error) === "function") {
                obj.error();
            }
            return false;
        });
    },
    localHttp: function (obj) {
        $axios({
            url: obj.url,
            method: obj.method || 'get',
            data: obj.form || obj.params || undefined,
            headers: obj.header || undefined,
        }).then((res) => {
            if (typeof (obj.newThen) === 'function') {
                obj.newThen();
            }
            if (res.status === 200) {
                let resData;
                resData = res.data;
                if (resData.code === 200) {
                    if (typeof (obj.success) === 'function') {
                        obj.success(resData);
                    }
                } else {
                    $P.Msg("返回数据异常！", 4);
                    if (typeof (obj.error) === "function") {
                        obj.error(res);
                    }
                }
                return false;
            } else {
                $P.Msg("异常HTTP错误：" + res.status, 4);
                if (typeof (obj.error) === "function") {
                    obj.error(res);
                }
                return false;
            }
        }).catch((error) => {
            $P.Msg("访问中断，程序出错！", 4);
            if (!$global.HTTP_CRYPTO) console.error("网络中断，程序出错！", error);

            if (typeof (obj.error) === "function") {
                obj.error();
            }
            return false;
        });
    }
}

export default ajax;